package com.example.boxugu.myapplication.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.boxugu.myapplication.Moder.ChangeUserInfoModer;
import com.example.boxugu.myapplication.Moder.UserInfoModer;
import com.example.boxugu.myapplication.R;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class UserInfoActivity extends AppCompatActivity {
    ImageButton imageButton;
    EditText musername,mnickname,mgrade,mdes;
    Button button;
    String token;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        imageButton=findViewById(R.id.imgbtn_back);
        musername=findViewById(R.id.yhmtv);
        mnickname=findViewById(R.id.nametv);
        mgrade=findViewById(R.id.sextv);
        mdes=findViewById(R.id.qmtv);
        button=findViewById(R.id.save_btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(UserInfoActivity.this, ChangeUserinfoActivity.class);
                startActivity(intent);
                finish();
            }
        });
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        final SharedPreferences sharedPreferences=UserInfoActivity.this.getSharedPreferences("boxugu", Context.MODE_PRIVATE);
        token=sharedPreferences.getString("token","");
        if (token.length()>0) {
            RequestParams params = new RequestParams("http://148.70.46.9:8080/boxuegu/bxg/getuserinfo");
            params.addBodyParameter("token", token);
            x.http().post(params, new Callback.CacheCallback<String>() {
                @SuppressLint("ResourceType")
                @Override
                public void onSuccess(String result) {
                    Gson gson = new Gson();
                    UserInfoModer userInfoModer = gson.fromJson(result, UserInfoModer.class);
                    if (userInfoModer != null && userInfoModer.data != null) {
                        musername.setText(userInfoModer.data.username);
                        mnickname.setText(userInfoModer.data.nickname);
                        mgrade.setText(userInfoModer.data.grade);
                        mdes.setText(userInfoModer.data.des);
                    }


                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {


                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }

                @Override
                public boolean onCache(String result) {
                    return false;
                }
            });
        }

    }
}
